#include <stdafx.h>
#include <testpobo/trafficWatch.h>
#include <qserver/client.hpp>
#include <qserver/io_service_pool.hpp>
//#include <testpobo/client.hpp>
#include <testpobo/client_work.hpp>
#include <testpobo/client_work_alloc.hpp>

using namespace std;

int main(int argc, char* argv[])
{
    unsigned short port;
    std::string host;
    boost::program_options::options_description desc("Allow options");
    desc.add_options()
        ("help,h", "help")
        ("host,H", boost::program_options::value<std::string>(&host), "Remote host")
        ("port,P", boost::program_options::value<unsigned short>(&port), "Remote port")
        ;
    try
    {
        boost::program_options::variables_map vm;
        boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
        boost::program_options::notify(vm);
        if(vm.count("help"))
        {
            cout << desc << "\n";
            exit(1);
        }
        if(0 == vm.count("port")||0 == vm.count("host"))
        {
            cout << "Usage: testpobo -H 127.0.0.1 -P 11668\n";
            cout << desc << "\n";
            exit(1);
        }
    }
    catch (std::runtime_error& e)
    {
    	cout << desc << "\n";
        exit(1);
    }
    catch (...)
    {
        cout << desc << "\n";
        exit(1);
    }
    PropertyConfigurator::configure("testpobo.log4cxx");
    LoggerPtr logger = Logger::getRootLogger();
    LOG4CXX_INFO(logger, "");
    LOG4CXX_INFO(logger, "************************************");
    LOG4CXX_INFO(logger, "***********TestPobo Traffic*********");
    LOG4CXX_INFO(logger, "************************************");
    LOG4CXX_INFO(logger, "");
    typedef qserv::client<testpobo::client_work, testpobo::client_work_allocator> client_type;
    typedef qserv::NetConnPool<testpobo::client_work, testpobo::client_work_allocator> client_conn_pool_type;
    qserv::io_service_pool ioPool(4);
    utils::TrafficWatchPtr trafficWatch(new utils::CTrafficWatch(ioPool.get_io_service(), logger));
    boost::shared_ptr<client_conn_pool_type> connPoolPtr(new client_conn_pool_type(boost::shared_ptr<testpobo::client_work_allocator>(new testpobo::client_work_allocator(logger, ioPool, trafficWatch)),
        1));
    client_type disClient(host, port, connPoolPtr);
    disClient.connect(ioPool.get_io_service(), ioPool.get_io_service(), 3);
    ioPool.run();
    return 0;
}
